package service;

import java.util.ArrayList;
import java.util.Date;

import org.bouncycastle.crypto.engines.ISAACEngine;

import domain.DeniedUser;

public class DeniedUserHolder {

	private ArrayList<DeniedUser> deniedUsers;
	private static DeniedUserHolder instance;
	private static final Object singletonObject = new Object();
	
	public DeniedUserHolder() {
		deniedUsers = new ArrayList<DeniedUser>();
	}
	
	public static DeniedUserHolder getInstance(){
		if(instance == null){
			synchronized (singletonObject) {
				if(instance == null){
					instance = new DeniedUserHolder();
				}
			}
		}
		return instance;
	}
	
	public void add(DeniedUser deniedUser){
		synchronized (this) {
			deniedUsers.add(deniedUser);
		}
	}
	
	public void remove(DeniedUser deniedUser){
		synchronized (this) {
			deniedUsers.remove(deniedUser);
		}
	}

	/**
	 * @return the deniedUsers
	 */
	public ArrayList<DeniedUser> getDeniedUsers() {
		return deniedUsers;
	}

	public void addToOldDeny(String username, int i) {
		for (int j = 0; j < deniedUsers.size(); j++) {
			if(deniedUsers.get(j).getName().equals(username)){
				deniedUsers.get(j).setDenied(deniedUsers.get(j).getDenied()+i);
			}
		}
		
	}
	
	public void addNewDeny(String username, int i){
		DeniedUser deniedUser;
		deniedUser = new DeniedUser(username, i);
		deniedUsers.add(deniedUser);
	}
	
	public boolean gibtesschon(String str){
		boolean check = false;
		
		for (int i = 0; i < deniedUsers.size(); i++) {
			if(deniedUsers.get(i).getName().equals(str)){
				check = true;
			}
		}
		
		return check;
	}
	
	public boolean gibtesschonThreeTimesUser(String str){
		boolean check = false;
		
		for (int i = 0; i < deniedUsers.size(); i++) {
			if(deniedUsers.get(i).getName().equals(str) && (deniedUsers.get(i).getDenied() >= 3)){
				check = true;
			}
		}
		
		return check;
	}
	
	public void deleteDeniedUser(String str){
		DeniedUser deniedUser;
		for (int i = 0; i < deniedUsers.size(); i++) {
			if(deniedUsers.get(i).getName().equals(str)){
				deniedUser = new DeniedUser(str, deniedUsers.get(i).getDenied());
				remove(deniedUser);
			}
		}
	}
	
	public boolean getThreeTimesDenied() {
		boolean check = false;
		
		for (int i = 0; i < deniedUsers.size(); i++) {
			if(deniedUsers.get(i).getDenied() >= 3){
				check = true;
			}
		}	
		
		return check;
	}
	
	

}
